package com.ruoyi.web.controller.resource;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.resource.domain.ResOpus;
import com.ruoyi.resource.service.IResOpusService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 作品Controller
 * 
 * @author ruoyi
 * @date 2021-12-20
 */
@RestController
@RequestMapping("/resource/opus")
public class ResOpusController extends BaseController
{
    @Autowired
    private IResOpusService resOpusService;

    /**
     * 查询作品列表
     */
    @PreAuthorize("@ss.hasPermi('resource:opus:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResOpus resOpus)
    {
        startPage();
        List<ResOpus> list = resOpusService.selectResOpusList(resOpus);
        return getDataTable(list);
    }

    /**
     * 用户查询作品列表
     */
    @GetMapping("/searchList")
    public TableDataInfo searchList(ResOpus resOpus)
    {
        startPage();
        List<ResOpus> list = resOpusService.selectResOpusSearchList(resOpus);
        return getDataTable(list);
    }

    /**
     * 查询用户部分作品
     */
    @GetMapping("/userContractionOpus")
    public TableDataInfo userContractionOpus(ResOpus resOpus)
    {
        startPage();
        resOpus.setUserId(getUserId());
        List<ResOpus> list = resOpusService.selectResOpusContractionOpus(resOpus);
        return getDataTable(list);
    }

    /**
     * 查询好友动态
     */
    @GetMapping("/feedList")
    public TableDataInfo feedList(ResOpus resOpus)
    {
        startPage();
        resOpus.setUserId(getUserId());
        List<ResOpus> list = resOpusService.selectResOpusNewsFeedOpus(resOpus);
        return getDataTable(list);
    }
    /**
     * 导出作品列表
     */
    @PreAuthorize("@ss.hasPermi('resource:opus:export')")
    @Log(title = "作品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResOpus resOpus)
    {
        List<ResOpus> list = resOpusService.selectResOpusList(resOpus);
        ExcelUtil<ResOpus> util = new ExcelUtil<ResOpus>(ResOpus.class);
        util.exportExcel(response, list, "作品数据");
    }

    /**
     * 获取作品详细信息
     */
    @PreAuthorize("@ss.hasPermi('resource:opus:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(resOpusService.selectResOpusById(id));
    }

    /**
     * 新增作品
     */
    @PreAuthorize("@ss.hasPermi('resource:opus:add')")
    @Log(title = "作品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResOpus resOpus)
    {
        resOpus.setCreateBy(getUsername());
        resOpus.setUserId(getUserId());

        return toAjax(resOpusService.insertResOpus(resOpus));
    }

    /**
     * 修改作品
     */
    @PreAuthorize("@ss.hasPermi('resource:opus:edit')")
    @Log(title = "作品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResOpus resOpus)
    {
        return toAjax(resOpusService.updateResOpus(resOpus));
    }

    /**
     * 修改作品浏览
     */
    @Log(title = "作品", businessType = BusinessType.UPDATE)
    @PutMapping("/editView")
    public AjaxResult editView(@RequestBody ResOpus resOpus)
    {
        return toAjax(resOpusService.updateResOpusView(resOpus));
    }

    /**
     * 修改作品分享
     */
    @Log(title = "作品", businessType = BusinessType.UPDATE)
    @PutMapping("/editShare")
    public AjaxResult editShare(@RequestBody ResOpus resOpus)
    {
        return toAjax(resOpusService.updateResOpusShare(resOpus));
    }

    /**
     * 修改作品下载
     */
    @Log(title = "作品", businessType = BusinessType.UPDATE)
    @PutMapping("/editDownload")
    public AjaxResult editDownload(@RequestBody ResOpus resOpus)
    {
        return toAjax(resOpusService.updateResOpusDownload(resOpus));
    }
    /**
     * 删除作品
     */
    @Log(title = "作品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(resOpusService.deleteResOpusByIds(ids));
    }
}
